<?php
/**
* @package Pages-and-Items (com_pi_pages_and_items)
* @version 1.5.7
* @copyright Copyright (C) 2006-2010 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}


?>
<link href="components/com_pi_pages_and_items/css/popup.css" rel="stylesheet" type="text/css" />
<link href="components/com_pi_pages_and_items/css/pages_and_items3.css" rel="stylesheet" type="text/css" />
<?php
if( !defined('_JEXEC') ){//joomla version is 1.0.x so also get those red-like colors
	echo '<link href="../../com_pi_pages_and_items/admin/components/com_pi_pages_and_items/css/joomla_1_0_x.css" rel="stylesheet" type="text/css" />';
}  
?>
<style type="text/css">

#imagewrapper{
	width: 700px;	
	overflow: hidden;	
}

#minwidth{
	min-width: 700px;
}

</style>
<script language="JavaScript" type="text/javascript">
<!--

function selectImage(imageNumber,imageSrc,imageWidth,imageHeight){
selectedImage = imageSrc;
imagePath = 'images/stories/'+imageSrc;
window.parent.document.getElementById('src').value = imagePath;
window.parent.document.getElementById('alt').value = imageSrc;
window.parent.document.getElementById('border').value = '0';
window.parent.document.getElementById('width').value = imageWidth;
window.parent.document.getElementById('height').value = imageHeight;
window.parent.showPreviewImage(imagePath);
if(currentImage!=imageNumber && currentImage!=0){
oldImage = 'image'+currentImage;
document.getElementById(oldImage).className = 'imageSelect';
}
currentImage = imageNumber;
imageDiv = 'image'+imageNumber;
document.getElementById(imageDiv).className = 'imageSelectOn';
radio = 'radio'+imageNumber;
document.getElementById(radio).checked = true;
}

function deleteImage(){
	if(selectedImage!=''){	
		if(confirm("<?php echo _pi_lang_confirm_image_delete; ?>")){		
			document.getElementById('fileToDelete').value = selectedImage;
			document.adminForm.submit(); 	
		}
	}else{
		alert ('<?php echo _pi_lang_no_file; ?>');
	}
} 
 
// set language for image popup
function set_language_in_popup(){
	window.parent.document.getElementById('upload_select').innerHTML = '<?php echo _pi_lang_upload_select; ?>';
	//window.parent.document.getElementById('extra_options').innerHTML = '<?php echo _pi_lang_extra_options; ?>';
	window.parent.document.getElementById('upload_image').innerHTML = '<?php echo _pi_lang_upload_image; ?>';
	window.parent.document.getElementById('select_image').innerHTML = '<?php echo _pi_lang_select_image; ?>';
}
document.onload = set_language_in_popup();
 
-->
</script>
<form name="adminForm" method="post" style="margin: 0; padding: 0;">
		<input type="hidden" name="option" value="com_pi_pages_and_items" />
		<input type="hidden" name="task" value="image_file_delete" />
		<input type="hidden" name="fileToDelete" id="fileToDelete" value="" />
		<div style="text-align: right;">
			<input type="button" value="<?php echo _pi_lang_delete_selected_image; ?>" onclick="deleteImage();" style="margin: 0 20px 20px 0;" />
		</div>
<?php

$dir = dirname(__FILE__)."/../../../../images/stories/";
if( defined('_JEXEC') ){
	//joomla 1.5
	$path = $live_site = JURI::root()."/images/stories/";
}else{
	//joomla 1.0.x
	$path = $mosConfig_live_site."/images/stories/";
}





$currentImage = 0;
$selectedImage = false;
$currentFileName = 0;
$exten = 'gif,png,jpg,jpeg'; // The extensions to display
if ($handle = opendir($dir)){
	$counter = 1;
    while (false !== ($file = readdir($handle))) {
       
		$extension = substr(strrchr($file, "."), 1);
		if (in_array($extension, explode(',', $exten))){            
				 
				$imageSize = getimagesize("$dir$file");
				$imageWidth = $imageSize[0];
				$imageHeight = $imageSize[1];				
				
				if ($imageWidth > 200){
					if ($imageHeight > 80){
						$imageDimension = 'height="80"';						
					}else{
						$imageDimension = 'width="200"';
					}
				}elseif ($imageHeight > 80){
					$imageDimension = 'height="80"';
				}else{
					$imageDimension = 'height="'.$imageHeight.'" width="'.$imageWidth.'"';
				}			
				
				echo '<table class="imageSelect';
				if($class_pi->get_var('image_uploaded', '' )==$file){
					 echo "On";
					 $currentImage = $counter;
					 $selectedImage = $file;
				} 
				echo "\" id=\"image$counter\" onclick=\"selectImage($counter,'$file',$imageWidth,$imageHeight);\" title=\""._pi_lang_file."=$file "._pi_lang_width."=$imageWidth "._pi_lang_height."=$imageHeight\">";	
				echo '<tr><td valign="middle" align="center" height="80">';
				if($class_pi->get_var('image_uploaded', '' )==$file){
					 echo '<a name="image" id="image"></a><br style="height: 0;line-height: 0;">';
				} 								
				echo "<img src=\"$path$file\" $imageDimension alt=\"\" />\n";
				echo "</td></tr><tr><td>\n";				
				echo "<input type=\"radio\" name=\"image\" id=\"radio$counter\" ";
				if($class_pi->get_var('image_uploaded', '' )==$file){
					echo "checked=true";
				}
				echo " />$file";
				echo "</td></tr>\n";			
				echo '</table>';               
        }
	$counter = $counter + 1;  
    } 
		 
	closedir($handle);
}
?>
<script language="JavaScript" type="text/javascript">
<!--

var currentImage = <?php if($currentImage){echo $currentImage;}else{echo '0';} ?>;
var selectedImage = "<?php if($selectedImage){echo $selectedImage;} ?>";


-->
</script>
</form>